package mathe;

public class Matrix{
  private double[][] c;//Zeile, Spalte

  public Matrix(int z, int s, double... werte){
    if(werte.length>z*s)
      throw new IllegalArgumentException("Zu viele Werte: "+werte.length);
    c=new double[z][s];
    for(int i=0; i<werte.length; i++)
      c[i/s][i%s]=werte[i];
  }

  public Matrix(int n, double... werte) {this(n, n, werte);}

  private Matrix(int z, int s) {c=new double[z][s];}

  public static Matrix spaltenvektor(double... werte){
    return new Matrix(werte.length, 1, werte);
  }

  public static Matrix zeilenvektor(double... werte){
    return new Matrix(1, werte.length, werte);
  }

  public int numRows(){return c.length;}

  public int numCols(){return c[0].length;}

  public String toString(){
    StringBuilder sb=new StringBuilder();
    for(int z=0; z<c.length; z++){
      double[] zeile=c[z];
      for(int s=0; s<zeile.length; s++)
        sb.append(zeile[s]+"\t");
      sb.append("\n");
    }
    return sb.toString();
  }

  public Matrix mult(Matrix m){
    int len=m.numRows();
    if(numCols()!=len)
      throw new IllegalArgumentException("Breite: "+numCols()+" Höhe: "+len);
    Matrix r=new Matrix(numRows(), m.numCols());
    for(int z=0; z<numRows(); z++)
      for(int s=0; s<m.numCols(); s++)
        for(int i=0; i<len; i++) r.c[z][s]+=c[z][i]*m.c[i][s];
    return r;
  }

  public static void main(String[] args) throws Exception{
    Matrix p=new Matrix(4,
      .125,.375,.375,.125,
      .375,.125,.375,.125,
      .625,.125,.125,.125, 
      .125,.125,.125,.625
      );
    Matrix x=zeilenvektor(.25,.25,.25,.25);
    System.out.println(x);
    for(int i=1; i<=10; i++){
      System.out.println(i+". Multiplikation:");
      x=x.mult(p);
      System.out.println(x);
    }
  }
}

